<?php

use \Mapper\MemberModel as MemberMapper;

class GamecircleController extends \Base\AbstractController {

    /**
     * @desc 群列表
     */
    public function listAction() {
        $request = $this->getRequest();
        $class   = (int)$request->get('class');
        $keyword = trim((string)$request->get('keyword'));
        $status  = $request->get('status');
        $return  = [];
        $where   = [];

        if(!empty($class) && !empty($keyword)) {
            if($class === 1) {
                $first = ['id'=>$keyword];
            }elseif($class === 2) {
                $first = ['name LIKE "%'.$keyword.'%"'];
            }
        }

        if(!empty($status)) {
            $second = ['status'=>$status];
        }

        //组装查询语句
        if(!empty($first) && !empty($second)) {
            $where = array_merge($first,$second);
        }elseif(!empty($first) || !empty($second)) {
            !empty($first) ? $where = $first : $where = $second;
        }

        $page = new \Lib\Paginator('gameCircle');
        $page->where($where);
        $this->assign('count', $page->count());
        $this->assign('page', $page->getPage());
        $this->assign('perPage', $page->getPerpage());
        $models = $page->lists();

        $memberModel =  MemberMapper::getInstance();

        foreach($models as $model) {
            $data              = $model->toArray();
            $data['nick_name'] = urldecode($memberModel->findById($data['member_id'])->getNick_name());
            $return[]          = $data;
        }

        $this->assign('lists', $return);
    }

    /**
     * @desc 群成员
    */
    public function memberListAction() {
        $request = $this->getRequest();
        $circleId = $request->get('id');
        $page = new \Lib\Paginator('gameCircleMember');
        $page->where(['game_circle_id' => $circleId]);
        $page->order('type ASC');
        $this->assign('count', $page->count());
        $this->assign('page', $page->getPage());
        $this->assign('perPage', $page->getPerpage());
        $models = $page->lists();
        $memberModel = MemberMapper::getInstance();
        $return = [];

        foreach($models as $model) {
            $data = $model->toArray();
            $member = $memberModel->findById($data['member_id']);

            if($member instanceof \MemberModel){
                $data['member_nick_name'] = urldecode($member->getNick_name());
            }

            $return[] = $data;
        }

        $this->assign('lists',$return);
    }

    /**
     * @desc 群资料详情
    */
    public function editAction() {
        $request          = $this->getRequest();
        $gameCircleId     = $request->get('id');
        $gameCircleMapper = \Mapper\GameCircleModel::getInstance();

        $model = $gameCircleMapper->findById($gameCircleId);

        $this->assign('model',$model);
    }

    /**
     * @desc 屏蔽群
    */
    public function delAction() {
        $request  = $this->getRequest();
        $circleId = (int)$request->get('cid');
        $status   = (int)$request->get('status');

        $gameCircleMapper = \Mapper\GameCircleModel::getInstance();

        if($status === 1) {
            $temp = 2;
        }elseif($status === 2) {
            $temp = 1;
        }

        try{
            $row = $gameCircleMapper->update(['status'=>$temp],['id'=>$circleId]);

            if($row > 0){
                return \Response::msg('修改成功！');
            }elseif($row == 0) {
                return \Response::msg('未修改！');
            }

        }catch (Exception $exc) {
            return \Response::errmsg($exc->getMessage());
        }
    }

    /**
     * @desc 一个所在的电竞圈列表
    */
    public function personAction() {
        $request          = $this->getRequest();
        $memberId         = $request->get('id');
        $gameCircleMapper = \Mapper\GameCircleModel::getInstance();
        $return           = [];

        $page = new \Lib\Paginator('gameCircleMember');
        $page->where(['member_id'=>$memberId]);
        $this->assign('count', $page->count());
        $this->assign('page', $page->getPage());
        $this->assign('perPage', $page->getPerpage());
        $models = $page->lists();

        foreach($models as $model) {
            $data = $model->toArray();
            $gameCircleModel =$gameCircleMapper->findById($data['game_circle_id']);
            $data['name'] =$gameCircleModel->getName();
            $data['status'] =$gameCircleModel->getStatus();
            $return[] = $data;
        }

        $this->assign('lists',$return);
    }
}
